//
//  LKMutableDatePicker.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/9.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKMutableDatePicker.h"
#import <Masonry/Masonry.h>
#import "NSString+Regex.h"

@interface LKMutableDatePicker()

@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) UIView *bottomView;
@property (nonatomic,copy) void(^dateBlock)(NSString *startTime,NSString *endTime);
@property (nonatomic,copy) void(^cancelBlock)();

@end

@implementation LKMutableDatePicker

+ (void) showCustomDatePickerAtView:(UIView *)superView
choosedDateBlock:(void (^)(NSString *,NSString *))date
                         cancelBlock:(void(^)())cancel{
    LKMutableDatePicker *picker = [[LKMutableDatePicker alloc] init];
    [superView addSubview:picker];
    picker.translatesAutoresizingMaskIntoConstraints = NO;
    [picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    if (date) {
        picker.dateBlock = date;
    }
    if (cancel) {
       picker.cancelBlock = cancel;
    }
}

//+(void)showCustomDatePickerAtView:(UIView *)superView choosedDateBlock:(void (^)(NSDate *))date cancelBlock:(void (^)())cancel{
//    LKMutableDatePicker *picker = [[LKMutableDatePicker alloc] init];
//    [superView addSubview:picker];
//    picker.translatesAutoresizingMaskIntoConstraints = NO;
//    [picker mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(superView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
//    }];
//    if (date) {
//        picker.dateBlock = date;
//    }
//    if (cancel) {
//       picker.cancelBlock = cancel;
//    }
//
//}
-(instancetype)init{
    if (self = [super init]) {
      self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self setupUI];
    }
    return self;
}
//设置视图的界面
-(void)setupUI{
    WS(weakSelf)
    self.datePicker = [[UIDatePicker alloc] init];
    _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    //定义最小日期
    NSDate *maxDate = [NSDate date];
    _datePicker.maximumDate = maxDate;
    NSDate *minDate = [NSString getPriousorDayLaterDateFromDate:maxDate withDay:-31];
//    NSDate *minDate = [NSString getPriousorLaterDateFromDate:maxDate withMonth:-1];
    _datePicker.minimumDate = minDate;
    [self addSubview:_datePicker];
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(180);
    }];
    self.bottomView = [[UIView alloc] init];
    [self addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(_datePicker.mas_top);
        make.height.mas_equalTo(40);
    }];
    UIColor *color = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:249/255.0 alpha:1];
    UIColor *btnColor = [UIColor colorWithRed:65.0/255 green:164.0/255 blue:249.0/255 alpha:1];
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:btnColor forState:0];
    [_bottomView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(_bottomView);
        make.width.mas_equalTo(60);
    }];
    UIButton *surelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [surelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [surelBtn setTitleColor:btnColor forState:0];
    [_bottomView addSubview:surelBtn];
    [surelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(_bottomView);
        make.width.mas_equalTo(60);
    }];
    cancelBtn.tag = 1010;
    surelBtn.tag = 1020;
    [surelBtn addTarget:self action:@selector(ChooseOrNot:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn addTarget:self action:@selector(ChooseOrNot:) forControlEvents:UIControlEventTouchUpInside];
    

    weakSelf.startTime = [NSString getTimeStrAccoringDate:[NSDate date]];
    weakSelf.endTime = [NSString getTimeStrAccoringDate:[NSDate date]];
    _startBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"开始时间" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        btn.selected = !btn.selected;
        weakSelf.endBtn.selected = NO;
        weakSelf.endTime = [NSString getTimeStrAccoringDate:weakSelf.datePicker.date];
        weakSelf.datePicker.date = [NSString getDateAccordingTimeStr:weakSelf.startTime];
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.redlineV.centerX = btn.centerX;
        }];
    }];
    _startBtn.selected = YES;
    [_startBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [_bottomView addSubview:_startBtn];
    [_startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_bottomView);
        make.bottom.equalTo(_bottomView.mas_bottom).offset(-5);
       make.centerX.equalTo(_bottomView.mas_centerX).offset(-50);
       make.width.mas_equalTo(100);
    }];
    _redlineV = [[UIView alloc] init];
    _redlineV.backgroundColor = [UIColor redColor];
    [_bottomView addSubview:_redlineV];
    [_redlineV mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_startBtn.mas_bottom).offset(4);
       make.centerX.equalTo(_startBtn.mas_centerX);
       make.width.mas_equalTo(60);
       make.height.mas_equalTo(1);
    }];
    _endBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"结束时间" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {        btn.selected = !btn.selected;
        weakSelf.startBtn.selected = NO;
        weakSelf.startTime = [NSString getTimeStrAccoringDate:weakSelf.datePicker.date];
        weakSelf.datePicker.date = [NSString getDateAccordingTimeStr:weakSelf.endTime];
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.redlineV.centerX = btn.centerX;
        }];
    }];
    [_endBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [_bottomView addSubview:_endBtn];
    [_endBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_bottomView);
        make.bottom.equalTo(_bottomView.mas_bottom).offset(-5);
       make.centerX.equalTo(_bottomView.mas_centerX).offset(50);
       make.width.mas_equalTo(100);
    }];
    _bottomView.backgroundColor = color;
    _datePicker.backgroundColor = color;
    
}
//按钮选择
-(void)ChooseOrNot:(UIButton *)btn{
    if (btn.tag == 1010) {
        if (self.cancelBlock) {
            self.cancelBlock();
        }
        [self removeFromSuperview];
    }
    if (btn.tag == 1020) {
        if (self.dateBlock) {
            if (self.startBtn.selected) {
                if ([[NSString getTimeStrAccoringDate:self.datePicker.date] compare:self.endTime] == NSOrderedAscending || [[NSString getTimeStrAccoringDate:self.datePicker.date] compare:self.endTime] == NSOrderedSame) {
                    self.dateBlock([NSString getTimeStrAccoringDate:self.datePicker.date],self.endTime);
                }else{
                    [SVProgressHUD showInfoWithStatus:@"开始时间不能晚于结束时间"];
                }

            }else{
                if ([self.startTime compare:[NSString getTimeStrAccoringDate:self.datePicker.date]] == NSOrderedAscending || [[NSString getTimeStrAccoringDate:self.datePicker.date] compare:self.endTime] == NSOrderedSame) {
                    self.dateBlock(self.startTime,[NSString getTimeStrAccoringDate:self.datePicker.date]);
                }else{
                    [SVProgressHUD showInfoWithStatus:@"开始时间不能晚于结束时间"];
                }

            }

        }
        [self removeFromSuperview];
    }
}

@end
